#include "Colors/RIVIABrightnessContrast.h"
#include "IRIVIAProject.h"
#include "opencv2/core.hpp"

RIVIABrightnessContrast::RIVIABrightnessContrast()
{

}

RIVIABrightnessContrast::~RIVIABrightnessContrast()
{

}

void RIVIABrightnessContrast::changeBrightnessContrast(double alpha, int beta, double gamma)
{
    cv::Mat src = IRIVIAProject::getInstance()->getCurMat();
    cv::Mat dst = IRIVIAProject::getInstance()->getCurMatProc();

    for( int y = 0; y < src.rows; y++ ) {
        for( int x = 0; x < src.cols; x++ ) {
            for( int c = 0; c < src.channels(); c++ ) {
                dst.at<cv::Vec3b>(y,x)[c] =
                  cv::saturate_cast<uchar>( alpha*src.at<cv::Vec3b>(y,x)[c] + beta );
            }
        }
    }

    cv::Mat lookUpTable(1, 256, CV_8U);
    uchar* p = lookUpTable.ptr();
    for( int i = 0; i < 256; ++i)
        p[i] = cv::saturate_cast<uchar>(pow(i / 255.0, gamma) * 255.0);
    cv::LUT(dst, lookUpTable, dst);

}

void RIVIABrightnessContrast::finishChange()
{
    IRIVIAProject::getInstance()->updateImageByImageProc();
}
